/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package as24gc.parser;

import as24gc.db.Vehicles;
import java.io.StringReader;
import java.util.Calendar;
import java.util.Date;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;

/**
 *
 * @author andreavenditti
 */
public class ParserUtility {

    public static InputObject runEngineParserSax(String source) {
        EngineParserSax parserSax = new EngineParserSax();
        try {
            StringReader sr = new StringReader(source);
            InputSource is = new InputSource(sr);
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            saxParser.parse(is, parserSax);
            System.out.println("Vehicle: " + parserSax.getV().toString());
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
        return parserSax.getInputObject();
    }

    public static Date parserDate(String s) {
        s = s.trim();
        int indexDelimiter = s.indexOf("/");
        int m = Integer.parseInt(s.substring(0, indexDelimiter));
        int y = Integer.parseInt(s.substring(indexDelimiter + 1, s.length()));
        System.out.println("m: " + m--);
        System.out.println("y: " + y);
        Calendar c = Calendar.getInstance();
        c.set(Calendar.MONTH, m);
        c.set(Calendar.YEAR, y);
        System.out.println("date: " + c.getTime());

        return c.getTime();
    }

    public static int parserKm(String a) {

        a = a.trim();
        int indexDelimiter = a.indexOf("k");
        String tmp = a.substring(0, indexDelimiter);
        System.out.println("tmp: " + tmp);
        int m = Integer.parseInt(tmp);
        System.out.println("m: " + m);
        return m;
    }

    public static int parserKw(String b) {

        System.out.println("input String to parserKw: "+b);
        b = b.trim();
        b = b.toUpperCase();
        int x = 0;
        if (b.contains("CV") && !b.contains("KW")) {
            System.out.println("in contains CV and not KW");
            int indexDelimiter = b.indexOf(" C");
            x = Integer.parseInt(b.substring(0, indexDelimiter));
            x = parserCV(x);
        } else {
            System.out.println("else");
            int indexDelimiter = b.indexOf(" K");
            x = Integer.parseInt(b.substring(0, indexDelimiter));
        }
        System.out.println(x);
        return x;
    }

    public static int parserCV(int b) {

        double d = b * 75 * 0.00980665;
        System.out.println("double: " + d);
        return (int) d;
    }

    public static String parserWorkAroundMemcar(String media) {
        media = media.trim();
        String mediaList[] = media.split(",");
        String newMedia = "";
        for (int i = 0; i < mediaList.length; i++) {
            System.out.println("mediaList[" + i + "]: " + mediaList[i]);
            newMedia += mediaList[i].substring(mediaList[i].indexOf("src=") + 5, mediaList[i].indexOf("?"));
            if (i < mediaList.length - 1) {
                newMedia += ";";
            }
        }

        System.out.println("newMedia: " + newMedia);

        return newMedia;


    }
}
